package org.ghost.wechat.platform.config;

import org.apache.commons.lang3.StringUtils;
import org.ghost.wechat.platform.common.redis.GhostKeyGenerator;
import org.ghost.wechat.platform.config.properties.RedisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration;
import org.springframework.session.web.http.DefaultCookieSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.util.Pool;

/**
 * @author Administrator
 */
@Configuration
public class RedisConfig {
    @Autowired
    private RedisProperties redisProperties;

    @Bean(value = "jedisPoolConfig")
    public JedisPoolConfig getJedisPoolConfig() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        if (redisProperties.getMaxIdle() != null) {
            poolConfig.setMaxIdle(redisProperties.getMaxIdle());
        }
        if (redisProperties.getMaxTotal() != null) {
            poolConfig.setMaxTotal(redisProperties.getMaxTotal());
        }
        if (redisProperties.getMaxWaitMillis() != null) {
            poolConfig.setMaxWaitMillis(redisProperties.getMaxWaitMillis());
        }
        if (redisProperties.getTestOnBorrow() != null) {
            poolConfig.setTestOnBorrow(redisProperties.getTestOnBorrow());
        }
        return poolConfig;
    }

    @Bean
    public Pool<Jedis> getJedisPool(JedisPoolConfig jedisPoolConfig) {
        return new JedisPool(jedisPoolConfig, this.redisProperties.getHost(), this.redisProperties.getPort(), this.redisProperties.getTimeout());
    }

    @Bean
    public GhostKeyGenerator getGhostKeyGenerator() {
        return new GhostKeyGenerator();
    }

    @Bean(value = "jedisConnectionFactory")
    public JedisConnectionFactory getJedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        if (StringUtils.isNotBlank(redisProperties.getHost())) {
            jedisConnectionFactory.setHostName(redisProperties.getHost());
        }
        if (redisProperties.getPort() != null) {
            jedisConnectionFactory.setPort(redisProperties.getPort());
        }
        jedisConnectionFactory.setPassword(redisProperties.getPass());
        jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
        if (redisProperties.getUsePool() != null) {
            jedisConnectionFactory.setUsePool(redisProperties.getUsePool());
        }
        if (redisProperties.getTimeout() != null) {
            jedisConnectionFactory.setTimeout(redisProperties.getTimeout());
        }

        return jedisConnectionFactory;
    }

    @Bean(value = "defaultCookieSerializer")
    public DefaultCookieSerializer getDefaultCookieSerializer() {
        DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer();
        defaultCookieSerializer.setCookieName("session-id");
        defaultCookieSerializer.setCookiePath("/");
        return defaultCookieSerializer;
    }

    @Bean(value = "redisHttpSessionConfiguration")
    public RedisHttpSessionConfiguration getRedisHttpSessionConfiguration(DefaultCookieSerializer defaultCookieSerializer) {
        RedisHttpSessionConfiguration redisHttpSessionConfiguration = new RedisHttpSessionConfiguration();
        redisHttpSessionConfiguration.setCookieSerializer(defaultCookieSerializer);
        redisHttpSessionConfiguration.setMaxInactiveIntervalInSeconds(1800);
        return redisHttpSessionConfiguration;
    }
}
